<?php
include_once('./incl/tk_wb_defs.php');

define('WINDOW_TITLE'	,	'AMFramework ToolKit GUI');
define('TK_NAME'		,	'AMFramework Toolkit');
define('TK_VER'			,	'v1.0');

define('TK_FRAME01_TITLE'	,	'Create a new application or MVC elements');
define('TK_FRAME02_TITLE'	,	'Toolkit Output:');
define('TK_FRAME03_TITLE'	,	'Help Area'		);
define('TK_LABEL_01_NAME'	,	'New:'			);
define('TK_LABEL_02_NAME'	,	'Inside:'		);
define('TK_LABEL_03_NAME'	,	'Name:'			);
define('TK_LABEL_04_NAME'	,	'Prefix:'		);
define('TK_LABEL_05_NAME'	,	'App Tmpl:'		);
define('TK_LABEL_06_NAME'	,	'Language:'		);
define('TK_LABEL_07_NAME'	,	'Copyright:'	);
define('TK_LABEL_08_NAME'	,	'Description:'	);
define('TK_LABEL_09_NAME'	,	'Keywords:'		);
define('TK_LABEL_10_NAME'	,	'Design Tmpl:'	);

define('TK_COMBO_0', 'Application From Template');
define('TK_COMBO_1', 'Empty Application');
define('TK_COMBO_2', 'New Controller & View');
define('TK_COMBO_3', 'New Model Class');
define('TK_COMBO_4', 'New Table Class');
define('TK_COMBO_5', 'Empty Module');

define('TK_BUTTON_01_NAME'	,	'Do it!'		);

function createMainWindow() 
{
	global $winmain, $controls;
	
	$winmain 	= wb_create_window(null,AppWindow,WINDOW_TITLE,WBC_CENTER,WBC_CENTER,700,460);
	$controls 	= array();
	
	/* =====Design controls===== */
	wb_create_control($winmain, Label, TK_NAME.' '.TK_VER, 300, 405, 300, 15, 0);
	
	// Frames
	$controls['static'] = array();
	$controls['static'][TK_FRAME_01CREATE] = wb_create_control($winmain, Frame, TK_FRAME01_TITLE, 5, 5, 460, 220, TK_FRAME_01CREATE);
	$controls['static'][TK_FRAME_02OUT]    = wb_create_control($winmain, Frame, TK_FRAME02_TITLE, 5, 230, 460, 170, TK_FRAME_02OUT);
	$controls['static'][TK_FRAME_03HELP]   = wb_create_control($winmain, Frame, TK_FRAME03_TITLE, 475, 5, 200, 395, TK_FRAME_03HELP);
	
	// Labels
	$controls['static'][TK_LABEL_01] = wb_create_control($winmain, Label, TK_LABEL_01_NAME, 40, 30, 25, 15, TK_LABEL_01);
	$controls['static'][TK_LABEL_02] = wb_create_control($winmain, Label, TK_LABEL_02_NAME, 265, 30, 40, 15, TK_LABEL_02, 0x00000001, 0, 0);
	$controls['static'][TK_LABEL_03] = wb_create_control($winmain, Label, TK_LABEL_03_NAME, 35, 55, 35, 15, TK_LABEL_03);
	$controls['static'][TK_LABEL_04] = wb_create_control($winmain, Label, TK_LABEL_04_NAME, 35, 80, 35, 15, TK_LABEL_04);
	$controls['static'][TK_LABEL_06] = wb_create_control($winmain, Label, TK_LABEL_05_NAME, 20, 105, 60, 15, TK_LABEL_06);
	$controls['static'][TK_LABEL_05] = wb_create_control($winmain, Label, TK_LABEL_06_NAME, 245, 80, 60, 15, TK_LABEL_05);
	$controls['static'][TK_LABEL_07] = wb_create_control($winmain, Label, TK_LABEL_07_NAME, 245, 105, 55, 15, TK_LABEL_07);
	$controls['static'][TK_LABEL_08] = wb_create_control($winmain, Label, TK_LABEL_08_NAME, 15, 130, 60, 15, TK_LABEL_08);
	$controls['static'][TK_LABEL_09] = wb_create_control($winmain, Label, TK_LABEL_09_NAME, 20, 190, 55, 15, TK_LABEL_09);
	$controls['static'][TK_LABEL_10] = wb_create_control($winmain, Label, TK_LABEL_10_NAME, 265, 55, 40, 15, TK_LABEL_10);
	
	// Read-only multi-line edit-boxes
	$controls['static'][TK_EDITREAD_02OUTP] = wb_create_control($winmain, EditBox, '', 15, 250, 445, 145, TK_EDITREAD_02OUTP, 0x000000C0, 0, 0);
	$controls['static'][TK_EDITREAD_01HELP] = wb_create_control($winmain, EditBox, '', 480, 25, 190, 340, TK_EDITREAD_01HELP, 0x000000C0, 0, 0);	

	/* =====Action-controls===== */
	
	// Combo-boxes
	$controls['combo'] = array();
	$controls['combo'][TK_COMBO_01NEW] 		= wb_create_control($winmain, ComboBox, '', 80, 30, 155, 145, TK_COMBO_01NEW,	0x00000040, 0, 0);
	$controls['combo'][TK_COMBO_02INSIDE] 	= wb_create_control($winmain, ComboBox, '', 305, 30, 120, 115, TK_COMBO_02INSIDE, 0x00000041, 0, 0);
	$controls['combo'][TK_COMBO_03TMPL] 	= wb_create_control($winmain, ComboBox, '', 80, 105, 120, 100, TK_COMBO_03TMPL, 0x00000040, 0, 0);
	$controls['combo'][TK_COMBO_04DESIGN] 	= wb_create_control($winmain, ComboBox, '', 305, 55, 120, 115, TK_COMBO_04DESIGN, 0x00000040, 0, 0);

	// Buttons
	$controls['static'][TK_BUTTON_01] 		= wb_create_control($winmain, PushButton, TK_BUTTON_01_NAME, 370, 190, 90, 25, TK_BUTTON_01);

	/* =====Focus controls===== */
	
	// Edit boxes
	$controls['edit'] = array();
	$controls['edit'][TK_EDIT_01NAME] = wb_create_control($winmain, EditBox, '', 80, 55, 155, 20, TK_EDIT_01NAME);
	$controls['edit'][TK_EDIT_02PREFIX] = wb_create_control($winmain, EditBox, '', 80, 80, 75, 20, TK_EDIT_02PREFIX);
	$controls['edit'][TK_EDIT_03LANG] = wb_create_control($winmain, EditBox, '', 305, 80, 75, 20, TK_EDIT_03LANG);
	$controls['edit'][TK_EDIT_04COPY] = wb_create_control($winmain, EditBox, '', 305, 105, 155, 20, TK_EDIT_04COPY);
	$controls['edit'][TK_EDIT_05DESC] = wb_create_control($winmain, EditBox, '', 80, 130, 380, 55, TK_EDIT_05DESC, 0x00000080, 0, 0);
	$controls['edit'][TK_EDIT_06KEYW] = wb_create_control($winmain, EditBox, '', 80, 190, 265, 20, TK_EDIT_06KEYW);
}

function setGuiInitialState() 
{
	global $winmain, $controls, $options_c02, $options_c03, $options_c04;

	// Initializarea optiunilor din dropdownboxuri; 
	// textul pentru constante este definit partea de sus
	$options = array(TK_COMBO_0, TK_COMBO_1, TK_COMBO_2, TK_COMBO_3, TK_COMBO_4, TK_COMBO_5);
	wb_set_text($controls['combo'][TK_COMBO_01NEW], $options);

	// Vom avea nevoie mai tarziu de arrayurile de mai jos;
	$options_c02 = _getFoldersFrom('../Apps/');
	wb_set_text($controls['combo'][TK_COMBO_02INSIDE], $options_c02);
	
	$options_c03 = _getFoldersFrom('./tmpl/');
	wb_set_text($controls['combo'][TK_COMBO_03TMPL], $options_c03);	

	$options_c04 = _getFoldersFrom('../Tmpl/');
	wb_set_text($controls['combo'][TK_COMBO_04DESIGN], $options_c04);
	
	// Fisierul ce va contine textul pentru Help
	if(file_exists('./help')) {
		wb_set_text($controls['static'][TK_EDITREAD_01HELP],file_get_contents('./help/help_gui'));
	}
}

function parentComboChanged() {
	global $winmain, $controls;
	
	$selectedIndex = wb_get_selected( $controls['combo'][TK_COMBO_01NEW] );
	_changeInsertState( $selectedIndex );
}

function _getFoldersFrom($dir) {
	if(!is_dir($dir)) { 
		trigger_error($dir." is not a valid directory. ", E_USER_NOTICE);
		return;
	}

	$options = array();
	if($dh = opendir($dir)) {
		while(($file = readdir($dh))!==false) {
			if(strrpos($file,'.')===false) {	// Excludem automat calea . si .. si eleminam orice fisiere;
				$options[] = $file;
			}
		}
		closedir($dh);
	}
	return $options;
}

function _changeInsertState( $selectedIndex ) 
{
	global $winmain, $controls;

	if($selectedIndex <= 1) 
	{
		$state = true;
		if( $selectedIndex == 1 ) 
		{	wb_set_enabled($controls['combo'][TK_COMBO_03TMPL], false);	}
		else 
		{	wb_set_enabled($controls['combo'][TK_COMBO_03TMPL], true);	}
	}
	else $state = false;	

	// Elementele isi inverseaza starea de vizibilitate 
	// in functie de variabila $state hotarata mai sus
	wb_set_visible($controls['static'][TK_LABEL_02], 		!$state);
	wb_set_visible($controls['combo'][TK_COMBO_02INSIDE], 	!$state);

	wb_set_visible($controls['static'][TK_LABEL_04], 	$state);
	wb_set_visible($controls['static'][TK_LABEL_05], 	$state);
	wb_set_visible($controls['static'][TK_LABEL_06], 	$state);
	wb_set_visible($controls['static'][TK_LABEL_07], 	$state);
	wb_set_visible($controls['static'][TK_LABEL_08], 	$state);
	wb_set_visible($controls['static'][TK_LABEL_09], 	$state);
	wb_set_visible($controls['static'][TK_LABEL_10], 	$state);
	
	wb_set_visible($controls['edit'][TK_EDIT_02PREFIX], $state);
	wb_set_visible($controls['edit'][TK_EDIT_03LANG], 	$state);
	wb_set_visible($controls['edit'][TK_EDIT_04COPY], 	$state);
	wb_set_visible($controls['edit'][TK_EDIT_05DESC], 	$state);
	wb_set_visible($controls['edit'][TK_EDIT_06KEYW], 	$state);

	wb_set_visible($controls['combo'][TK_COMBO_03TMPL], 	$state);
	wb_set_visible($controls['combo'][TK_COMBO_04DESIGN], 	$state);
}



/* --------------------------------------------------- */